package com.gaoxk.cxf.client.c20022000016;

import com.alibaba.fastjson.JSONObject;
import org.apache.cxf.endpoint.Client;
import org.apache.cxf.endpoint.ClientImpl;
import org.apache.cxf.frontend.ClientProxy;
//import org.apache.cxf.interceptor.LoggingInInterceptor;
//import org.apache.cxf.interceptor.LoggingOutInterceptor;
import org.apache.cxf.ext.logging.LoggingInInterceptor;
import org.apache.cxf.ext.logging.LoggingOutInterceptor;
import org.apache.cxf.transport.http.HTTPConduit;
import org.apache.cxf.transports.http.configuration.HTTPClientPolicy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.xml.ws.soap.SOAPFaultException;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

//@Service
public class Cxf20022000016Client {
//    private  final S30062000020 factory = new S30062000020();
//    private  final ESBServerPortType esbServerSoapEndpoint = factory.getESBServerSoapEndpoint();
//    private  final ObjectFactory objectFactory = new ObjectFactory();
//    private  final Logger logger = LoggerFactory.getLogger(Cxf30062000020Client.class);
 private  Logger logger = LoggerFactory.getLogger(Cxf20022000016Client.class);

    public  Map<String,Object> op72(String str){
        //System.out.println("1231231");


        logger.info("进程id：" + String.valueOf(getProcessID()));
        S20022000016 factory = new S20022000016();
         ESBServerPortType esbServerSoapEndpoint = factory.getESBServerSoapEndpoint();
         ObjectFactory objectFactory = new ObjectFactory();








        logger.info("请求开始");
        //组装头系统头
        ReqSysHeadType sysHeadType = objectFactory.createReqSysHeadType();

        sysHeadType.setReqSeq("1234567890");
        sysHeadType.setServiceID("20022000016");
        sysHeadType.setChannelID("1111000");
        sysHeadType.setLegOrgID("9999");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String format = simpleDateFormat.format(new Date());
        sysHeadType.setReqDate(format);
        SimpleDateFormat simple = new SimpleDateFormat("HHmmss");
        String s = simple.format(new Date());
        sysHeadType.setReqTime(s);
        sysHeadType.setReqSysID("B28");
        sysHeadType.setAcceptLang("zh_CN");
        sysHeadType.setGlobalSeq("1234567890");
        sysHeadType.setOrgSysID("B28");
        sysHeadType.setSvcScn("72");
        try {
            sysHeadType.setDomainRef(InetAddress.getLocalHost().getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        ReqLocalHead1Type reqLocalHead1Type =objectFactory.createReqLocalHead1Type();
        reqLocalHead1Type.setUSERID("12345689");
        reqLocalHead1Type.setSEQNO("13432432423"); ;
        reqLocalHead1Type.setCHANNELID("1111000");  ;
        reqLocalHead1Type.setTRANDATE("20250929");  ;
        reqLocalHead1Type.setTRANTIME("154700"); ;

        //组装应用头数据
        ReqAppHeadType reqAppHeadType = objectFactory.createReqAppHeadType();
        reqAppHeadType.setBrId("9900");
        reqAppHeadType.setTlrNo("");
        //组装body数据
        Req2002200001672Type.ReqAppBody appBody = objectFactory.createReq2002200001672TypeReqAppBody();
        appBody.setMsgType("0");
        appBody.setMsgContent(str);

        appBody.setRemark("访问ceshifilerToHere");




        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String format1 = dateFormat.format(new Date());
        appBody.setSendTime(format1);
        //合并报文
        Req2002200001672Type type = new Req2002200001672Type();
        type.setReqSysHead(sysHeadType);

        type.setReqAppHead(reqAppHeadType);
        type.setReqAppBody(appBody);
        logger.info("开始发送报文");
        ClientImpl client = (ClientImpl)ClientProxy.getClient(esbServerSoapEndpoint);
        //对于服务端返回的增加了字段，而客户端没有跟随修订，为了不报错，去掉xml的校验
        client.getRequestContext().put("set-jaxb-validation-event-handler", false);
//        client.getInInterceptors().add(new ClientInInterceptor());
//        client.getOutInterceptors().add(new ClientOutInterceptor());
        client.getInInterceptors().add(new LoggingInInterceptor());
        client.getOutInterceptors().add(new LoggingOutInterceptor());
        //流读取超时 ，毫秒，如果policy.setReceiveTimeout(10000);捕获异常时间是10000+1000毫秒
        //client.setSynchronousTimeout(1000);
        client.setSynchronousTimeout(0);



        Rsp2002200001672Type rsp2002200001672Type = null;
        try{
            Client proxy = ClientProxy.getClient(esbServerSoapEndpoint);
            HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
            HTTPClientPolicy policy = new HTTPClientPolicy();
 //连接超时时间
            policy.setConnectionTimeout(60000);
// 请求超时时间
            //policy.setReceiveTimeout(30000);

            conduit.setClient(policy);

//            Yaml yaml = new Yaml();
//            InputStream inputStream = Cxf30062000020Client.class.getClassLoader().getResourceAsStream("application.yml");
//
//            Map<String,Object> data = yaml.load(inputStream);
//            if(data != null) {
//                long connection_timeout = (long) data.get("cxf.client.connection-timeout");
//                long receive_timeout = (long) data.get("cxf.client.receive-timeout");
//                policy.setReceiveTimeout(connection_timeout);
//                policy.setConnectionTimeout(receive_timeout);
//            }
//            conduit.setClient(policy);
            rsp2002200001672Type = esbServerSoapEndpoint.op72(type);
        }
        catch (SOAPFaultException e){

            logger.error("超时",e);
            logger.error(e.getFault().getFaultCode());
            logger.error(e.getFault().getFaultString());
            //logger.error(e.getFault().getFaultCode(),e.getFault().getFaultString());
        }

        logger.info("返回报文结束,返回报文内容" + JSONObject.toJSONString(rsp2002200001672Type));
        Map<String,Object> map = new HashMap<>();
        map.put("dealcode",rsp2002200001672Type.getRspSysHead().getRetCode());
        logger.info("esb返回code" + rsp2002200001672Type.getRspSysHead().getRetCode());
        map.put("dealmsg",rsp2002200001672Type.getRspSysHead().getRetMsg());
        logger.info("esb返回msg" + rsp2002200001672Type.getRspSysHead().getRetMsg());

        String re = JSONObject.toJSONString(map);
        logger.info("esb参数结果输出------------------------------------------" + re);
        return map;
    }
    //获取进程id
    public  int getProcessID() {
        RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
        System.out.println(runtimeMXBean.getName());
        return Integer.valueOf(runtimeMXBean.getName().split("@")[0])
                .intValue();
    }
    public  Map<String,Object> op73(String str){
        //System.out.println("1231231");


        logger.info("进程id：" + String.valueOf(getProcessID()));
        S20022000016 factory = new S20022000016();
        ESBServerPortType esbServerSoapEndpoint = factory.getESBServerSoapEndpoint();
        ObjectFactory objectFactory = new ObjectFactory();








        logger.info("请求开始");
        //组装头系统头
        ReqSysHeadType sysHeadType = objectFactory.createReqSysHeadType();

        sysHeadType.setReqSeq("1234567890");
        sysHeadType.setServiceID("20022000016");
        sysHeadType.setChannelID("1111000");
        sysHeadType.setLegOrgID("9999");
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        String format = simpleDateFormat.format(new Date());
        sysHeadType.setReqDate(format);
        SimpleDateFormat simple = new SimpleDateFormat("HHmmss");
        String s = simple.format(new Date());
        sysHeadType.setReqTime(s);
        sysHeadType.setReqSysID("B28");
        sysHeadType.setAcceptLang("zh_CN");
        sysHeadType.setGlobalSeq("1234567890");
        sysHeadType.setOrgSysID("B28");
        sysHeadType.setSvcScn("73");
        try {
            sysHeadType.setDomainRef(InetAddress.getLocalHost().getHostAddress());
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        ReqLocalHead1Type reqLocalHead1Type =objectFactory.createReqLocalHead1Type();
        reqLocalHead1Type.setUSERID("12345689");
        reqLocalHead1Type.setSEQNO("13432432423"); ;
        reqLocalHead1Type.setCHANNELID("访问了ceshifilerToHere");  ;
        reqLocalHead1Type.setTRANDATE("20250929");  ;
        reqLocalHead1Type.setTRANTIME("154700"); ;

        //组装应用头数据
        ReqAppHeadType reqAppHeadType = objectFactory.createReqAppHeadType();
        reqAppHeadType.setBrId("9900");
        reqAppHeadType.setTlrNo("");
        //组装body数据
        Req2002200001673Type.ReqAppBody appBody = objectFactory.createReq2002200001673TypeReqAppBody();
        for (int i = 21; i < 30; i++){
            Req2002200001673Type.ReqAppBody.HEAD head = objectFactory.createReq2002200001673TypeReqAppBodyHEAD();
            head.setTRANSID(String.valueOf(i));
            head.setTRADECODE("1234567890");
            head.setBGORG("1234567890");
            head.setUSERID("1234567890");
            head.setBGUSER("1234567890");
            head.setNODENAME("1234567890");
            head.setBRNO("1234567890");
            head.setRUNSCRIPT("1234567890");
            head.setBACKUP0("1234567890");
            head.setBACKUP1("1234567890");
            head.setBACKUP2("1234567890");
            head.setBACKUP3("1234567890");
            head.setBACKUP4("1234567890");
            appBody.getHEAD().add(head);
        }

        for (int i = 31; i < 40; i++){
            Req2002200001673Type.ReqAppBody.BODY body = objectFactory.createReq2002200001673TypeReqAppBodyBODY();
            body.setTASKID(String.valueOf(i));
            body.setCALLTIME("1234567890");
            body.setCONTIDENT("1234567890");
            body.setRESULTT("1234567890");
            body.setBACKUP5("1234567890");
            body.setBACKUP6("1234567890");
            body.setBACKUP7("1234567890");
            body.setBACKUP8("1234567890");
            body.setBACKUP9("1234567890");
            appBody.getBODY().add(body);

        }



        //合并报文
        Req2002200001673Type req2002200001673type = new Req2002200001673Type();
        req2002200001673type.setReqSysHead(sysHeadType);
        req2002200001673type.setReqLocalHead(reqLocalHead1Type);
        req2002200001673type.setReqAppHead(reqAppHeadType);
        req2002200001673type.setReqAppBody(appBody);
        logger.info("开始发送报文");
        ClientImpl client = (ClientImpl)ClientProxy.getClient(esbServerSoapEndpoint);
        //对于服务端返回的增加了字段，而客户端没有跟随修订，为了不报错，去掉xml的校验
        client.getRequestContext().put("set-jaxb-validation-event-handler", false);
//        client.getInInterceptors().add(new ClientInInterceptor());
//        client.getOutInterceptors().add(new ClientOutInterceptor());
        client.getInInterceptors().add(new LoggingInInterceptor());
        client.getOutInterceptors().add(new LoggingOutInterceptor());
        //流读取超时 ，毫秒，如果policy.setReceiveTimeout(10000);捕获异常时间是10000+1000毫秒
        //client.setSynchronousTimeout(1000);
        client.setSynchronousTimeout(0);



        Rsp2002200001673Type rsp2002200001673Type = null;
        try{
            Client proxy = ClientProxy.getClient(esbServerSoapEndpoint);
            HTTPConduit conduit = (HTTPConduit) proxy.getConduit();
            HTTPClientPolicy policy = new HTTPClientPolicy();
            //连接超时时间
            policy.setConnectionTimeout(90000);
            // 请求超时时间
            policy.setReceiveTimeout(90000);

            conduit.setClient(policy);

//            Yaml yaml = new Yaml();
//            InputStream inputStream = Cxf30062000020Client.class.getClassLoader().getResourceAsStream("application.yml");
//
//            Map<String,Object> data = yaml.load(inputStream);
//            if(data != null) {
//                long connection_timeout = (long) data.get("cxf.client.connection-timeout");
//                long receive_timeout = (long) data.get("cxf.client.receive-timeout");
//                policy.setReceiveTimeout(connection_timeout);
//                policy.setConnectionTimeout(receive_timeout);
//            }
//            conduit.setClient(policy);
            rsp2002200001673Type = esbServerSoapEndpoint.op73(req2002200001673type);
        }
        catch (SOAPFaultException e){

            logger.error("超时",e);
            logger.error(e.getFault().getFaultCode());
            logger.error(e.getFault().getFaultString());
            //logger.error(e.getFault().getFaultCode(),e.getFault().getFaultString());
        }


        Map<String,Object> map = new HashMap<>();
        map.put("dealcode",rsp2002200001673Type.getRspSysHead().getRetCode());
        logger.info("esb返回code" + rsp2002200001673Type.getRspSysHead().getRetCode());
        map.put("dealmsg",rsp2002200001673Type.getRspSysHead().getRetMsg());
        logger.info("esb返回msg" + rsp2002200001673Type.getRspSysHead().getRetMsg());

        String re = JSONObject.toJSONString(map);
        logger.info("esb参数结果输出------------------------------------------" + re);
        return map;
    }


}
